﻿using System.Windows;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System;

namespace Silverlight.InputCtrl
{

    #region ValidatorCollection

    public class ValidateChangedEventArgs : EventArgs
    {
        private bool _ValidResult = false;

        public ValidateChangedEventArgs(bool Status)
        {
            _ValidResult = Status;
        }
        public bool ValidResult
        {
            get { return _ValidResult; }
        }
    }

    public class ValidatorCollection : ObservableCollection<IBaseValidator>
    {
        private bool _Loaded = false;
        private FrameworkElement _elemValidate;
        private Brush _saveBorder = null;
        private Thickness? _saveBorderThickness = null;
        private object _saveTooltip = null;
        private Exception[] _ExceptionsValidators = null;

        public string PropertyToValidate { set; get;}

        public Brush ElementBorder
        {
            get { return _saveBorder; }
        }

        public Thickness ElementBorderThickness
        {
            get { return _saveBorderThickness.Value; }
        }

        public object ElementTooltip
        {
            get { return _saveTooltip; }
        }

        internal void InitCollection(FrameworkElement element,Exception[] ExcepVld)
        {
            _elemValidate = element;
            _elemValidate.Loaded += new RoutedEventHandler(_elemValidate_Loaded);
            _ExceptionsValidators = ExcepVld;
        }

        private void _elemValidate_Loaded(object sender, RoutedEventArgs e)
        {
            if (!_Loaded)
            {
                if (_ExceptionsValidators.Length > 0)
                {
                    string MsgExcep = "";
                    foreach (var item in _ExceptionsValidators)
                    {
                        MsgExcep += item.Message + ";";
                    }
                    MsgExcep.TrimEnd(new char[] { ';' });
                    throw new Exception(MsgExcep);
                }

                FrameworkElement fe = (FrameworkElement)sender;
                Behaviortrigger bt = fe.GetValue(ValidatorService.behaviorProperty) as Behaviortrigger;
                if (bt == null)
                {
                    bt = new Behaviortrigger();
                    bt.OnChanged = false;
                    bt.Onleave = true;
                    bt.MessageTooltip = true;
                    bt.BorderIndicator = true;
                    fe.SetValue(ValidatorService.behaviorProperty, bt);
                }
                if (bt.BorderIndicator)
                {
                    _saveBorder = ((Control)fe).BorderBrush;
                    _saveBorderThickness = ((Control)fe).BorderThickness;
                }
                if (bt.MessageTooltip)
                {
                    object tooltip = ToolTipService.GetToolTip(fe);
                    if (tooltip != null)
                    {
                        _saveTooltip = tooltip;
                    }
                }
                if (bt.OnChanged)
                {
                    ValidatorCommon.addEventChanged(fe);
                }
                if (bt.Onleave)
                {
                    ValidatorCommon.addEventLeave(fe);
                }
                _Loaded = true;
            }
            _elemValidate.Loaded -= _elemValidate_Loaded;
        }
    }

    #endregion

    #region ContextValidator

    internal class ContextValidator
    {

        public ContextValidator(FrameworkElement Context)
        {
            ContextElement = Context;
            elementMessages = new List<ErroMessageElementInfo>();
            elementValidators = new List<IBaseValidator>();
        }
        public FrameworkElement ContextElement { get; set; }
        public List<ErroMessageElementInfo> elementMessages { get; set; }
        public List<IBaseValidator> elementValidators { get; set; }
    }

    #endregion

    #region ErroMessageElementInfo

    internal class ErroMessageElementInfo
    {
        public FrameworkElement ElementMessage { get; set; }
        public ValidatorErrorMessage ErrorMessage { get; set; }
    }

    #endregion

}
